const router = require('koa-router')()
const nodemail = require("../utils/nodemailer")
const { Sequelize } = require('sequelize')

router.prefix('/froms')

const sequelize = new Sequelize('stage5', 'root', 'mysql', {
    host: 'localhost',
    dialect: 'mysql'
});

sequelize.sync({ force: false })

const User = sequelize.define('user', {
    id: {
        type: Sequelize.UUID,
        unique: true,
        primaryKey: true,
        allowNull: false
    },
    name: {
        type: Sequelize.STRING
    },
    user: {

        type: Sequelize.STRING
    },
    pass: {
        type: Sequelize.STRING
    },
    img: {
        type: Sequelize.STRING
    },
    email: {
        type: Sequelize.STRING
    },
    date: {
        type: Sequelize.STRING
    },
    code: {
        type: Sequelize.STRING
    },
    miao: {
        type: Sequelize.STRING
    },
    dates: {
        type: Sequelize.STRING
    },
    flag: {
        type: Sequelize.STRING
    },
    jifen: {
        type: Sequelize.STRING
    },
    biaoqian: {
        type: Sequelize.STRING
    },
    dengji: {
        type: Sequelize.STRING
    },
    flages: {
        type: Sequelize.STRING
    },
    gender: {
        type: Sequelize.ENUM('MALE', 'FEMALE')
    }
}, {
    freezeTableName: true,
    timestamps: false
})

const Data = sequelize.define('orderdata'/*自定义表名*/, {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,       //主键
        autoIncrement: true,    //自增
        comment: "自增id"       //注释:只在代码中有效
    },
    name: {
        type: Sequelize.STRING
    },
    img: {
        type: Sequelize.STRING
    },
    isCheck: {
        type: Sequelize.STRING
    },
    num: {
        type: Sequelize.INTEGER
    },
    price: {
        type: Sequelize.INTEGER
    },
    state: {
        type: Sequelize.STRING
    },
    flag: {
        type: Sequelize.STRING
    },
    page: {
        type: Sequelize.STRING
    },
    Js: {
        type: Sequelize.STRING
    },
    Xq: {
        type: Sequelize.STRING
    },
    Pj: {
        type: Sequelize.STRING
    },
    oldid: {
        type: Sequelize.STRING
    },
    time: {
        type: Sequelize.STRING
    }
}, {
    freezeTableName: true,
    timestamps: false
})

const ShopDom = sequelize.define('ashop'/*自定义表名*/, {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,       //主键
        autoIncrement: true,    //自增
        comment: "自增id"       //注释:只在代码中有效
    },
    name: {
        type: Sequelize.STRING
    },
    img: {
        type: Sequelize.STRING
    },
    title: {
        type: Sequelize.STRING
    },
    time: {
        type: Sequelize.STRING
    },
    times: {
        type: Sequelize.STRING
    },
    mobile: {
        type: Sequelize.STRING
    },
    address: {
        type: Sequelize.STRING
    },
    addre: {
        type: Sequelize.STRING
    }
}, {
    freezeTableName: true,
    timestamps: false
})

const Dend = sequelize.define('dend'/*自定义表名*/, {
    id: {
        type: Sequelize.STRING,
        primaryKey: true,       //主键
        autoIncrement: true,    //自增
        comment: "自增id"       //注释:只在代码中有效
    },
    name: {
        type: Sequelize.STRING
    },
    gender: {
        type: Sequelize.STRING
    },
    phone: {
        type: Sequelize.STRING
    },
    addres: {
        type: Sequelize.STRING
    },
    house: {
        type: Sequelize.STRING
    },
    flag: {
        type: Sequelize.STRING
    }
}, {
    freezeTableName: true,
    timestamps: false
})

// 生成6位验证码
function createSixNum() {
    var Num = "";
    for (var i = 0; i < 6; i++) {
        Num += Math.floor(Math.random() * 10);
    }
    return Num;
}

router.get('/bar', async (ctx, next) => {
    let { id } = ctx.query
    let list = await User.findAll()
    let obj = list.find(item => {
        return item.id == id
    })
    ctx.body = obj
})

router.get('/youxiang', async (ctx, next) => {
    let email = ctx.query.you;//刚刚从前台传过来的邮箱
    let id = JSON.parse(ctx.query.id);
    let code = await createSixNum();//这里是我写的生成的随机六位数，等等下面给代码
    let date = new Date();//获取当前时间
    //去数据库中找有没有同名的用户名，这里就要自己写了，不同的数据库查询方法不同
    let list = await User.findAll()
    let index = list.findIndex(item => {
        return item.email === email
    })

    if (index >= 0) {
        ctx.body = "邮箱已注册"
    } else {
        let mail = {
            // 发件人
            from: 'z3063537882@163.com>',
            // 主题
            subject: '接受凭证',//邮箱主题
            // 收件人
            to: email,//前台传过来的邮箱
            // 邮件内容，HTML格式
            text: `您的验证码是${code}。`//发送验证码
        };

        let pram = { 'date': JSON.stringify(date), 'code': code };
        await User.update(
            pram, { 'where': { 'id': id } }
        )
        await nodemail(mail);//发送邮件
        ctx.body = "验证码已发送"
    }
})

router.get('/xiang', async (ctx, next) => {
    let id = ctx.query.id
    let code = ctx.query.xiang;//获取你输入的验证码
    let email = ctx.query.you;

    //去数据库把刚刚在存验证码的时候一起存的那条记录找出来
    let list = await User.findAll()
    let obj = list.find(item => {
        return item.id == id
    })
    let nowDate = (new Date()).getTime();//获取当前时间

    // 判断验证码是否正确，时间是否超过1分钟
    if (obj.code !== code) {
        ctx.body = { ids: 1, msg: "验证码不正确" }
    } else if (nowDate - (new Date(JSON.parse(obj.date)).getTime()) > 60000) {
        ctx.body = { ids: 1, msg: "验证码已过期" }
    } else {
        let pram = { 'email': email };
        await User.update(
            pram, { 'where': { 'id': id } }
        )
        ctx.body = { ids: 2, msg: "邮箱已绑定" }
    }
})

router.get('/ding', async (ctx, next) => {
    let { id } = ctx.query
    const [results, metadata] = await sequelize.query('select * from orderdata', { raw: true })
    if (id == 0) {
        ctx.body = results
    } else {
        let data = results.filter(item => {
            return item.state == id
        })
        ctx.body = data
    }
})

router.get('/dingdan', async (ctx, next) => {
    const [results, metadata] = await sequelize.query('select * from orderdata', { raw: true })
    ctx.body = results
})

router.get('/shouhuo', async (ctx, next) => {
    let { id } = ctx.query
    await Data.update({ state: 4 }, { where: { id: id } })
    ctx.body = '成功收货'
})

router.get('/shanchu', async (ctx, next) => {
    let { id } = ctx.query
    await Data.destroy({ 'where': { id: id } })
    ctx.body = '成功删除'
})

router.get('/huiyuan', async (ctx, next) => {
    let { id, ids } = ctx.query
    let miao;
    if (ids === "0") {
        miao = 1000 * 60 * 60 * 24 * 3
    } else if (ids === "1") {
        miao = 1000 * 60 * 60 * 24 * 7
    } else if (ids === "2") {
        miao = 1000 * 60 * 60 * 24 * 30
    }
    let nowDate = new Date()//获取当前时间

    await User.update({ miao: miao, dates: JSON.stringify(nowDate), flag: true }, { where: { id: id } })
    let list = await User.findAll()
    let obj = list.find(item => {
        return item.id == id
    })
    ctx.body = obj
})

router.get('/xinxi', async (ctx, next) => {
    let { id } = ctx.query
    let list = await User.findAll()
    let obj = list.find(item => {
        return item.id == id
    })
    if (obj.dates && obj.dates !== "") {
        let nowDate = (new Date()).getTime();//获取当前时间
        if (nowDate - (new Date(JSON.parse(obj.dates)).getTime()) < Number(obj.miao)) {
            ctx.body = obj
        } else {
            await User.update({ flag: false }, { where: { id: id } })
            obj.flag = 0
            ctx.body = obj
        }
    } else {
        ctx.body = obj
    }
})

router.get('/miao', async (ctx, next) => {
    let id = JSON.parse(ctx.query.id)
    await User.update({ miao: "", flages: 'false' }, { where: { id: id } })
    let list = await User.findAll()
    let obj = list.find(item => {
        return item.id == id
    })
    ctx.body = obj
})

router.get('/diyi', async (ctx, next) => {
    let id = JSON.parse(ctx.query.id)
    await User.update({ flages: 'true' }, { where: { id: id } })
    ctx.body = 111
})

router.get('/tongzhi', async (ctx, next) => {
    let id = JSON.parse(ctx.query.id)
    await User.update({ flages: 'false' }, { where: { id: id } })
    let list = await User.findAll()
    let obj = list.find(item => {
        return item.id == id
    })
    ctx.body = obj
})

router.get('/fukuan', async (ctx, next) => {
    let { id } = ctx.query
    await Data.update({ state: 3 }, { where: { id: id } })
    ctx.body = '成功付款'
})

router.get('/user', async (ctx, next) => {
    let list = await User.findAll()
    list.sort(function (a, b) { return Number(a.id) - Number(b.id) })
    ctx.body = list
})

router.get('/shop', async (ctx, next) => {
    let list = await ShopDom.findAll()
    ctx.body = list
})

router.get('/tijiao', async (ctx, next) => {
    let obj = ctx.query
    await ShopDom.update(obj, { where: { id: 1 } })
    ctx.body = '修改成功'
})

router.get('/qing', async (ctx, next) => {
    let id = ctx.query.id
    let list = await User.findAll()
    list.sort(function (a, b) { return Number(a.id) - Number(b.id) })
    let obj = list.find(item => {
        return item.id == id
    })
    ctx.body = obj
})

router.get('/jiaru', async (ctx, next) => {
    let id = ctx.query.id
    await User.update({ biaoqian: "黑名单" }, { where: { id: id } })
    ctx.body = 1
})

router.get('/yichu', async (ctx, next) => {
    let id = ctx.query.id
    await User.update({ biaoqian: "-" }, { where: { id: id } })
    ctx.body = 1
})

router.get('/dizhi', async (ctx, next) => {
    let list = await Dend.findAll()
    ctx.body = list
})

router.get('/tiandizhi', async (ctx, next) => {
    let obj = ctx.query
    if (obj.flag === 'true') {
        let list = await Dend.findAll()
        for (let i = 0; i < list.length; i++) {
            await Dend.update({ flag: "false" }, { where: { id: i + 1 } })
        }
        await Dend.create(obj)
    } else {
        await Dend.create(obj)
    }
    ctx.body = 1
})

router.get('/chadizhi', async (ctx, next) => {
    let id = ctx.query.id;
    let list = await Dend.findAll()
    let obj = list.find(item => {
        return item.id == id
    })
    ctx.body = obj
})
module.exports = router